package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.zjitc.entity.Goods;
import net.zjitc.entity.GoodsAlbum;
import net.zjitc.entity.dto.GoodsDTO;
import net.zjitc.entity.vo.GoodsVO;
import net.zjitc.mapper.GoodsAlbumMapper;
import net.zjitc.service.GoodsService;
import net.zjitc.mapper.GoodsMapper;
import net.zjitc.utils.Result;
import net.zjitc.utils.ResultCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author win10
* @description 针对表【vueshop_goods】的数据库操作Service实现
* @createDate 2024-12-20 15:26:19
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
    implements GoodsService{
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsAlbumMapper goodsAlbumMapper;

    @Override
    public Result getGoodsList(GoodsVO goodsVO) {
        //1.分页参数
        IPage<Goods> page = new Page<>(goodsVO.getPage(),goodsVO.getPagesize());

        goodsMapper.selectPage(page,null);
        //2.提取商品ID列表
        List<Integer> goodsIds = page.getRecords().stream()
                .map(Goods::getId)
                .collect(Collectors.toList());

        //3.查询相册数据
        List<GoodsAlbum> albums=goodsAlbumMapper.selectAlbumsByGoodsIds(goodsIds);

        //4.按商品ID分组相册数据
        Map<Integer, List<String>> albumMap = albums.stream()
                .collect(Collectors.groupingBy(GoodsAlbum::getGoodsId,
                        Collectors.mapping(GoodsAlbum::getPicture,Collectors.toList())));
        //5.将相册数据组装到GoodsDTD
        List<GoodsDTO> goodsDTOList = page.getRecords().stream().map(
                goods -> {
                    GoodsDTO dto = new GoodsDTO();
                    BeanUtils.copyProperties(goods, dto);
                    dto.setAlbum(albumMap.getOrDefault(goods.getId(), Collections.emptyList()));
                    return dto;
                }
        ).collect(Collectors.toList());

        //结果封装
        //分页数据封装
        Map<String,Object> pageInfo=new HashMap<>();
        pageInfo.put("pageData",goodsDTOList);
        pageInfo.put("pageNum",page.getCurrent());
        pageInfo.put("pageSize",page.getSize());
        pageInfo.put("totalPage",page.getPages());
        pageInfo.put("totalSize",page.getTotal());

        Map<String,Object> pageInfoMap=new HashMap<>();
        pageInfo.put("pageInfo" ,pageInfo);
        //响应Json

        return Result.build(pageInfoMap,200,"获取商品列表成功");
    }

    @Override
    public Result getGoodsById(Integer id) {
        Goods goods = goodsMapper.selectById(id);
        if(goods!=null){
            return Result.build(goods,200,"获取商品详情成功");
        }
        return Result.build(null, ResultCodeEnum.SELECT_ERROR);
    }

    @Override
    public Result addGoods(GoodsDTO goods) {
        int ok = goodsMapper.insert(goods);
        if(ok>0){
            if (goods.getAlbum() != null && !goods.getAlbum().isEmpty()) {
                List<GoodsAlbum> albums = goods.getAlbum().stream()
                        .map(picture -> {
                            GoodsAlbum album = new GoodsAlbum();
                            album.setGoodsId(goods.getId()); // 获取插入后生成的商品ID
                            album.setPicture(picture);
                            return album;
                        }).collect(Collectors.toList());
                goodsAlbumMapper.insertBatch(albums); // 批量插入相册信息
            }
            return Result.build(goods , 200, "添加商品成功");
        }
        return Result.build(null , ResultCodeEnum.ADD_ERROR);
    }

    @Override
    public Result saveGoods(GoodsDTO goods) {
        //2.更新商品主表信息
        int ok=goodsMapper.updateById(goods);
        if(ok<0){
            return Result.build(null, ResultCodeEnum.UPDATE_ERROR);
        }

        //3.处理商品相册（good_album表)
        if(goods.getAlbum()!=null && !goods.getAlbum().isEmpty()){
            //删除旧的相册信息
            goodsAlbumMapper.deleteByGoodsId(Long.valueOf(goods.getId()));

            //插入新的相册信息
            List<GoodsAlbum> albums=goods.getAlbum().stream()
                    .map(picture->{
                        GoodsAlbum album = new GoodsAlbum();
                        album.setGoodsId(goods.getId());
                        album.setPicture(picture);
                        return album;
                    }).collect(Collectors.toList());
            goodsAlbumMapper.insertBatch(albums);
        }
        //返回成功结果
        return Result.build(goods, 200, "更新商品成功");
    }
}




